package pe.gob.osce.sirenas.wd.jsf.bean.registro;

import java.awt.Color;
import java.awt.Font;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import nl.captcha.Captcha;
import nl.captcha.backgrounds.GradiatedBackgroundProducer;
import nl.captcha.servlet.CaptchaServletUtil;
import nl.captcha.text.renderer.DefaultWordRenderer;

/**
 * Servlet implementation class SimpleCaptcha
 */
@WebServlet("/Simple.jpg")
public class SimpleCaptcha extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SimpleCaptcha() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        List<Color> colors = new ArrayList<Color> ();
        colors.add(Color.black);
       // colors.add(Color.red);
        response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
        response.setHeader("Pragma", "no-cache"); // HTTP 1.0.
        response.setDateHeader("Expires", 0); // Proxies.
        response.setCharacterEncoding("UTF-8");
        
        List<Font> fonts = new ArrayList<Font>();
        fonts.add(new Font("Arial", Font.ITALIC, 40));
         
        Captcha captcha = new Captcha.Builder(120, 50)
                .addText(new DefaultWordRenderer(colors, fonts))
                .addBackground(new GradiatedBackgroundProducer())
                .gimp()
                .addBorder()
                .build();
 
        // display the image produced
        CaptchaServletUtil.writeImage(response, captcha.getImage());
        
        request.setCharacterEncoding("UTF-8");
        // save the captcha value on session
        request.getSession().setAttribute(captcha.NAME, captcha);
        
        
        
        
//        Captcha captcha = new Captcha.Builder(120,40).addText().addBorder().gimp().addBackground(new GradiatedBackgroundProducer()).build();
//
//        CaptchaServletUtil.writeImage(response, captcha.getImage());
//
//        request.getSession().setAttribute("simpleCaptcha", captcha);
    }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
